package com.project.game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import java.util.LinkedList;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class MainScene extends JPanel implements Runnable {
	
	Thread frameloop;
	Image playerImage, obstacleImage, healImage, explosionImage, explosionImage2, backgroundImage ;
	Player player, bg;
	Point target;
	
	LinkedList<Obstacle> obstacles;
	//LinkedList<Heal> heals;
	LinkedList<Explosion> explosions;
	LinkedList<Explosion2> explosions2;
	Point position = new Point();
	
	int direction;
	int obstacleInterval;
	long lastTime, lastUpdateTime;
	
	private static final int DIRECTION_LEFT = 1;
	private static final int DIRECTION_RIGHT = 2;
	private static final int DIRECTION_UP = 3;
	private static final int DIRECTION_DOWN = 4;
	
	@Override
	public void addNotify() {
		super.addNotify();
		setBackground(Color.black);

		try {
			URL url = getClass().getResource("/ab.png"); // 지구 불러오기
			Image originalImage = ImageIO.read(url);
			playerImage = originalImage.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
			player = new Player(this, playerImage);
			
			url = getClass().getResource("/copy1.jpg"); // 배경 불러오기
			originalImage = ImageIO.read(url);
			backgroundImage = originalImage.getScaledInstance(800, 600, Image.SCALE_SMOOTH);
			
		    url = getClass().getResource("/FireSprite3.png");
			originalImage = ImageIO.read(url);
			obstacleImage = originalImage.getScaledInstance(256, 256, Image.SCALE_SMOOTH);
			obstacles = new LinkedList<Obstacle>();
			
			url = getClass().getResource("/explosion3.png");
			explosionImage2 = ImageIO.read(url);
			explosions2 = new LinkedList<Explosion2>();
			
			url = getClass().getResource("/explosion4.png");
			originalImage = ImageIO.read(url);
			explosionImage = originalImage.getScaledInstance(350, 350, Image.SCALE_SMOOTH);
			explosions = new LinkedList<Explosion>();
			
		} catch(Exception ex) {}
		
		frameloop =  new Thread(this);
		frameloop.start();
		
		target = new Point(400, 300);
		addMouseListener(mouseListener);
	}
	
	public void update(){
		long currentTime = System.currentTimeMillis();
		if(lastTime + 1000 < currentTime){	
			Point point = null;
			direction = (int)(Math.random() * 4)+1;
			switch(direction) {
			case DIRECTION_LEFT :
				point = new Point(-obstacleImage.getWidth(null), 
						(int) (Math.random() * (getHeight()+obstacleImage.getHeight(null))) -obstacleImage.getHeight(null));
				break;
			case DIRECTION_RIGHT :
				point = new Point(getWidth(), 
						(int) (Math.random() * (getHeight()+obstacleImage.getHeight(null))) -obstacleImage.getHeight(null));
				break;
			case DIRECTION_UP :
				point = new Point((int) (Math.random() * (getWidth())) -obstacleImage.getWidth(null), 
						-obstacleImage.getHeight(null));
				break;
			case DIRECTION_DOWN :
				point = new Point((int) (Math.random() * (getWidth())) -obstacleImage.getWidth(null), 
						getHeight());
				break;
			}
				
			Obstacle obstacle = new Obstacle(this, obstacleImage, point);
			
			synchronized(obstacles) {
				obstacles.add(obstacle);
			}
			lastTime = currentTime;
		}
		
		for (int i = obstacles.size() - 1; i >= 0; i--) {
			Obstacle obstacle = obstacles.get(i);
			obstacle.update();
			// 충돌 범위(
			if(obstacle.getPosition().x > 290 && obstacle.getPosition().x < 390 &&
					obstacle.getPosition().y > 190 && obstacle.getPosition().y < 290){
				// 지구 충돌 구현
				Obstacle removedObstacles = obstacles.remove(i);
				Explosion explosion = new Explosion(this, explosionImage, 9);
				Point p = removedObstacles.getPosition();
				int minusx = (explosionImage.getWidth(null) - obstacleImage.getWidth(null)) / 14; // 폭발 위치
				int minusy = (explosionImage.getHeight(null) - obstacleImage.getHeight(null)) / 14; // 폭발 위치
				explosion.setPosition(new Point(p.x-minusx , p.y-minusy)); // 폭발 위치(장애물의 가운데쯤)
				explosions.add(explosion);
				obstacle.setRemove(true);
				break;
			}
		}
		// 지구 충돌 폭발후 제거
		for (int i = explosions.size() - 1; i >= 0; i--) {
			explosions.get(i).update();
			if (explosions.get(i).isRemove()) {
				explosions.remove(i);
			}
		}
		
		// 마우스 충돌 폭발후 제거
		for (int i = explosions2.size() - 1; i >= 0; i--) {
			explosions2.get(i).update();
			if (explosions2.get(i).isRemove()) {
				explosions2.remove(i);
			}
		}
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(backgroundImage, 0, 0, null);
		synchronized (obstacles) {
			for (Obstacle obstacle : obstacles) {
				obstacle.render(g);
			}
		}
		player.render(g);
		for (Explosion explosion : explosions) {
			explosion.render(g);
		}
		for (Explosion2 explosion2 : explosions2) {
			explosion2.render(g);
		}
	}
	
	MouseListener mouseListener = new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			for (int i = obstacles.size() - 1; i >= 0; i--) {
				Obstacle obstacle = obstacles.get(i);
				double widthMin = obstacles.get(i).getPosition().getX() + 20;
				double widthMax = obstacles.get(i).getPosition().getX() + 80;
				double heightMin = obstacles.get(i).getPosition().getY() + 20;
				double heightMax = obstacles.get(i).getPosition().getY() + 80;
				double mouseX = getMousePosition().getX();
				double mouseY = getMousePosition().getY();
				if (widthMin <= mouseX && widthMax > mouseX
						&& heightMin <= mouseY && heightMax > mouseY) {
					// 마우스 충돌 구현
					Obstacle removedObstacles = obstacles.remove(i);
					Explosion2 explosion2 = new Explosion2(this, explosionImage2, 6);
					Point p = removedObstacles.getPosition();
					int minusx = (explosionImage2.getWidth(null) - obstacleImage.getWidth(null)) / 14; // 폭발 위치
					int minusy = (explosionImage2.getHeight(null) - obstacleImage.getHeight(null)) / 14; // 폭발 위치
					explosion2.setPosition(new Point(p.x-minusx , p.y-minusy)); // 폭발 위치(장애물의 가운데쯤)
					explosions2.add(explosion2);
					obstacles.remove(obstacle); // 목록에서 제거
				}
			}

		};
	};

	@Override
	// 쓰레드
	public void run() {
		while(true) {
			try {
				long currentTime = System.currentTimeMillis();
				if(lastUpdateTime + 30 < currentTime) {
					//1. update (데이터 변경)
					update();
					//2. rendering (화면 출력)
					repaint(); //update() -> paint() : UI 쓰레드에서 처리
					lastUpdateTime = currentTime;
				}
			} catch (Exception ex) {}
		}
	}
}

